package com.example.redisdemo.controller;

/*
* redis可视化
* String模块
* */


import com.example.redisdemo.model.K_V;
import com.example.redisdemo.model.RedisInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
public class StringController {

//    public static void main(String[] args) {
//        listAll();
//
//    }
    RedisInfo ri = new RedisInfo();
   @RequestMapping(value = {"allkeys","/"})
    public  ModelAndView listAll(){
       System.out.println(ri.getRIp());
       Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());

        Set<String> keySets = new HashSet<>();
        keySets = jedis.keys("*");
        List<K_V> keyList = new ArrayList<>();
        for (String s:keySets){
            K_V k_v = new K_V(s,jedis.type(s));
            keyList.add(k_v);

        }
        System.out.println(keySets);

        //按集合中的k_v对象的RedisKey值排序
        keyList.sort(Comparator.comparing(k_v -> k_v.getRedisType()));

        ModelAndView mvc = new ModelAndView("allkeys");
        mvc.addObject("set",keyList);
        return mvc;
    }
    @RequestMapping(value = {"string_list"})
    public  ModelAndView allStrings(){
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());

        Set<String> keySets = new HashSet<>();
        keySets = jedis.keys("*");
        List<Map<String,String>> strList = new ArrayList<>();
        for (String s:keySets){
            if(jedis.type(s).equals("string")){
               Map<String,String> map = new HashMap();
               map.put(s,jedis.get(s));
                strList.add(map);
            }
        }
        ModelAndView mvc = new ModelAndView("string_list");
        mvc.addObject("strList",strList);
        return mvc;
    }

    @RequestMapping(value = {"delString"})
    public  ModelAndView delStrings(HttpServletRequest request){
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());

        Set<String> keySets = new HashSet<>();
        String k = request.getParameter("redisKey");
        System.out.println(k);
        jedis.del(k);
//        keySets = jedis.keys("*");
//        List<Map<String,String>> strList = new ArrayList<>();
//        for (String s:keySets){
//            if(jedis.type(s).equals("string")){
//                Map<String,String> map = new HashMap();
//                map.put(s,jedis.get(s));
//                strList.add(map);
//            }
//        }
        ModelAndView mvc = new ModelAndView("string_list");
//        mvc.addObject("strList",strList);
        return mvc;
    }

    @RequestMapping(value = {"addString"})
    public  ModelAndView addString(@RequestParam("rkey")String rkey,@RequestParam("rvalue")String rvalue){
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());

        System.out.println(rkey+" "+rvalue);
        jedis.set(rkey,rvalue);

        Set<String> keySets = new HashSet<>();

        keySets = jedis.keys("*");
        List<Map<String,String>> strList = new ArrayList<>();
        for (String s:keySets){
            if(jedis.type(s).equals("string")){
                Map<String,String> map = new HashMap();
                map.put(s,jedis.get(s));
                strList.add(map);
            }
        }
        ModelAndView mvc = new ModelAndView("string_list");
        mvc.addObject("strList",strList);
        return mvc;
    }
    @RequestMapping(value = {"editString"})
    public  ModelAndView editString(@RequestParam("key")String rkey,@RequestParam("rvalue")String rvalue){
        Jedis jedis = new Jedis(ri.getRIp(),ri.getRPort());

        System.out.println(rkey+" "+rvalue);
        jedis.set(rkey,rvalue);

        Set<String> keySets = new HashSet<>();

        keySets = jedis.keys("*");
        List<Map<String,String>> strList = new ArrayList<>();
        for (String s:keySets){
            if(jedis.type(s).equals("string")){
                Map<String,String> map = new HashMap();
                map.put(s,jedis.get(s));
                strList.add(map);
            }
        }
        ModelAndView mvc = new ModelAndView("string_list");
        mvc.addObject("strList",strList);
        return mvc;
    }





}
